package com.june.junemplayer.viewModel;

import androidx.lifecycle.AndroidViewModel;

import com.june.junemplayer.base.BaseApplication;
import com.june.junemplayer.pojo.Song;
import com.june.junemplayer.pojo.SongList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class GlobalViewModel extends AndroidViewModel {

    //本地歌曲，即所有的歌曲列表
    private ArrayList<Song> mSongs=new ArrayList<>();
    private Map<String, Song> mSongsMap=new HashMap<>();
    //用户创建的歌单
    private ArrayList<SongList> mCustomSongList=new ArrayList<>();
    private Map<String, SongList> mCustomSongListMap=new HashMap<>();
    //默认歌单，分别是"最近播放"，"我的喜欢"
    private Map<String, SongList> mDefaultSongListMap=new HashMap<>();
    //播放列表
    private ArrayList<Song> mPlayingList=new ArrayList<>();


    public GlobalViewModel() {
        super(BaseApplication.getApplication());
    }

    public void init(ArrayList<Song> songs, ArrayList<SongList> customSongLists, ArrayList<SongList> defaultSongLists) {
        mSongs = songs;
        mCustomSongList = customSongLists;
        for (Song song : songs) {
            mSongsMap.put(song.getPath(), song);
        }
        for (SongList songList : customSongLists) {
            mCustomSongListMap.put(songList.getTitle(), songList);
        }
        for (SongList songList : defaultSongLists) {
            mDefaultSongListMap.put(songList.getTitle(), songList);
        }
    }

    public ArrayList<Song> getSongs() {
        return mSongs;
    }

    public Map<String, Song> getSongsMap() {
        return mSongsMap;
    }

    public ArrayList<SongList> getCustomSongList() {
        return mCustomSongList;
    }

    public Map<String, SongList> getCustomSongListMap() {
        return mCustomSongListMap;
    }

    public Map<String, SongList> getDefaultSongListMap() {
        return mDefaultSongListMap;
    }

    public ArrayList<Song> getPlayingList() {
        return mPlayingList;
    }

    public void addSongs(ArrayList<Song> songs) {
        mSongs.addAll(songs);
        for (Song song : songs) {
            mSongsMap.put(song.getPath(), song);
        }
    }

    public void addSong(Song song) {
        mSongs.add(song);
        mSongsMap.put(song.getPath(), song);
    }

//    public void removeSongs(ArrayList<Song> songs) {
//        for (Song song : songs) {
//            mSongs.remove(song);
//        }
//    }

    @Override
    protected void onCleared() {
        super.onCleared();
        mSongs = null;
        mSongsMap = null;
        mCustomSongList = null;
        mCustomSongListMap = null;
        mDefaultSongListMap = null;
    }
}